package cz.zcu.fav.rat.rest.factory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cz.zcu.fav.rat.beans.Period;
import cz.zcu.fav.rat.rest.beans.PeriodMapping;
import cz.zcu.fav.rat.rest.beans.PeriodsMapping;

@Component
public class PeriodsMappingFactory extends
		AbstractMappingFactory<PeriodsMapping, Collection<Period>>
{

	@Autowired
	PeriodMappingFactory periodMappingFactory;

	@Override
	public PeriodsMapping getObject()
	{
		return new PeriodsMapping();
	}

	@Override
	public Class<?> getObjectType()
	{
		return PeriodsMapping.class;
	}

	@Override
	public PeriodsMapping getMappedObject(Collection<Period> object)
	{
		PeriodsMapping periodsMapping = getObject();
		List<PeriodMapping> mappedPeriods = new ArrayList<PeriodMapping>();
		for (Period period : object)
		{
			mappedPeriods.add(periodMappingFactory.getMappedObject(period));
		}
		periodsMapping.setPeriods(mappedPeriods);
		return periodsMapping;
	}

	@Override
	public Collection<Period> getRemappedObject(PeriodsMapping object, boolean ignoreId)
	{
		Collection<Period> periods = new HashSet<Period>();
		for (PeriodMapping periodMapping : object.getPeriods())
		{
			periods.add(periodMappingFactory.getRemappedObject(periodMapping, ignoreId));
		}
		return periods;
	}

}
